﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.TextScraping
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PdfSectionFilterEditor : Form, IComponentEditor
    {
        private object _ActivityNode;
        private PdfSectionFilterComponent _SectionFilterComponent;

        /// <summary>
        /// 
        /// </summary>
        public PdfSectionFilterEditor()
        {
            InitializeComponent();
        }

        #region Implementation of IComponentEditor

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode = value; }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get { return typeof(PdfSectionFilterComponent); }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents, object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._SectionFilterComponent = (PdfSectionFilterComponent) wfComponent;

            this.txt_SrcConnStr.Text = this._SectionFilterComponent.ConnStr;
            this.rt_SelectSql.Text = this._SectionFilterComponent.SelectSql;
            this.cbo_SrcIDField.Items.Clear();
            this.cbo_FilePathField.Items.Clear();
            if(!string.IsNullOrEmpty(this.txt_SrcConnStr.Text) && !string.IsNullOrEmpty(this.rt_SelectSql.Text))
            {
                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, this.txt_SrcConnStr.Text,
                                                            this.rt_SelectSql.Text);
                if(cols !=null && cols.Length>0)
                {
                    foreach(TableColumn col in cols)
                    {
                        this.cbo_SrcIDField.Items.Add(col.ColumnName);
                        this.cbo_FilePathField.Items.Add(col.ColumnName);
                    }
                    this.SelectFieldInComboBox(this.cbo_SrcIDField, this._SectionFilterComponent.IDField);
                    this.SelectFieldInComboBox(this.cbo_FilePathField,this._SectionFilterComponent.FilePathField);
                }
            }

            this.txt_TgtConnStr.Text = this._SectionFilterComponent.TgtConnStr;
            this.cbo_TgtTables.Items.Clear();
            if(!string.IsNullOrEmpty(this.txt_TgtConnStr.Text))
            {
                string[] tblNames = DbUtil.GetTableNames(DataProviderType.MSSQL, string.Empty, this.txt_TgtConnStr.Text);
                if(tblNames !=null && tblNames.Length>0)
                {
                    foreach(string tblName in tblNames)
                    {
                        this.cbo_TgtTables.Items.Add(tblName);
                    }

                    this.SelectFieldInComboBox(this.cbo_TgtTables, this._SectionFilterComponent.TgtTableName);

                    this.SelectFieldInComboBox(this.cbo_TgtIDField,this._SectionFilterComponent.TgtIDField);
                    this.SelectFieldInComboBox(this.cbo_ContentField, this._SectionFilterComponent.TgtContentField);
                    this.SelectFieldInComboBox(this.cbo_FlagField,this._SectionFilterComponent.FlagField);
                }
            }

            if(this._SectionFilterComponent.HeaderPatterns !=null && this._SectionFilterComponent.HeaderPatterns.Count>0)
            {
                this.rt_HeaderPatterns.Text = StringUtil.ToString(
                    this._SectionFilterComponent.HeaderPatterns.ToArray(), Environment.NewLine);
            }
            else
            {
                this.rt_HeaderPatterns.Text = string.Empty;
            }
            if(this._SectionFilterComponent.FooterPatterns !=null && this._SectionFilterComponent.FooterPatterns.Count>0)
            {
                this.rt_FooterPatterns.Text = StringUtil.ToString(
                    this._SectionFilterComponent.FooterPatterns.ToArray(), Environment.NewLine);
            }
            else
            {
                this.rt_FooterPatterns.Text = string.Empty;
            }
            this.ck_AcrossMultiPages.Checked = this._SectionFilterComponent.AcrossMultiPages;
        }

        /// <summary></summary>
        public void SaveSettings()
        {
            this._SectionFilterComponent.ConnStr = this.txt_SrcConnStr.Text;
            this._SectionFilterComponent.SelectSql = this.rt_SelectSql.Text;
            this._SectionFilterComponent.IDField = this.cbo_SrcIDField.Text;
            this._SectionFilterComponent.FilePathField = this.cbo_FilePathField.Text;

            this._SectionFilterComponent.TgtConnStr = this.txt_TgtConnStr.Text;
            this._SectionFilterComponent.TgtTableName = this.cbo_TgtTables.Text;
            this._SectionFilterComponent.TgtIDField = this.cbo_TgtIDField.Text;
            this._SectionFilterComponent.TgtContentField = this.cbo_ContentField.Text;
            this._SectionFilterComponent.FlagField = this.cbo_FlagField.Text;

            this._SectionFilterComponent.HeaderPatterns=new List<string>();
            this._SectionFilterComponent.FooterPatterns=new List<string>();
            string[] headerPatterns = StringUtil.Split(this.rt_HeaderPatterns.Text, Environment.NewLine);
            if(headerPatterns !=null && headerPatterns.Length>0)
            {
                this._SectionFilterComponent.HeaderPatterns.AddRange(headerPatterns);
            }
            string[] footerPatterns = StringUtil.Split(this.rt_FooterPatterns.Text, Environment.NewLine);
            if(footerPatterns !=null && footerPatterns.Length>0)
            {
                this._SectionFilterComponent.FooterPatterns.AddRange(footerPatterns);
            }
            this._SectionFilterComponent.AcrossMultiPages = this.ck_AcrossMultiPages.Checked;
        }

        /// <summary></summary>
        public TestResult Test()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        private void SelectFieldInComboBox(ComboBox comboBox, string item)
        {
            if (comboBox.Items.Count > 0 && !string.IsNullOrEmpty(item))
            {
                for (int i = 0; i < comboBox.Items.Count; i++)
                {
                    if (comboBox.Items[i].ToString() == item)
                    {
                        comboBox.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void cbo_TgtTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_TgtTables.SelectedIndex>=0)
            {
                this.cbo_TgtIDField.Items.Clear();
                this.cbo_ContentField.Items.Clear();
                this.cbo_FlagField.Items.Clear();

                string tblName = this.cbo_TgtTables.SelectedItem.ToString();
                string connStr = this.txt_TgtConnStr.Text;
                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, connStr, string.Empty, tblName);
                if(cols !=null && cols.Length>0)
                {
                    foreach(TableColumn col in cols)
                    {
                        this.cbo_TgtIDField.Items.Add(col.ColumnName);
                        this.cbo_ContentField.Items.Add(col.ColumnName);
                        this.cbo_FlagField.Items.Add(col.ColumnName);
                    }
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._ActivityNode, this._SectionFilterComponent);

            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_RefreshSrcField_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txt_SrcConnStr.Text) && !string.IsNullOrEmpty(this.rt_SelectSql.Text))
            {
                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, this.txt_SrcConnStr.Text,
                                                            this.rt_SelectSql.Text);
                if (cols != null && cols.Length > 0)
                {
                    foreach (TableColumn col in cols)
                    {
                        this.cbo_SrcIDField.Items.Add(col.ColumnName);
                        this.cbo_FilePathField.Items.Add(col.ColumnName);
                    }
                    this.SelectFieldInComboBox(this.cbo_SrcIDField, this._SectionFilterComponent.IDField);
                    this.SelectFieldInComboBox(this.cbo_FilePathField, this._SectionFilterComponent.FilePathField);
                }
            }
        }

        private void btn_RefreshTables_Click(object sender, EventArgs e)
        {
            this.cbo_TgtTables.Items.Clear();
            if (!string.IsNullOrEmpty(this.txt_TgtConnStr.Text))
            {
                string[] tblNames = DbUtil.GetTableNames(DataProviderType.MSSQL, string.Empty, this.txt_TgtConnStr.Text);
                if (tblNames != null && tblNames.Length > 0)
                {
                    foreach (string tblName in tblNames)
                    {
                        this.cbo_TgtTables.Items.Add(tblName);
                    }

                    this.SelectFieldInComboBox(this.cbo_TgtTables, this._SectionFilterComponent.TgtTableName);

                    this.SelectFieldInComboBox(this.cbo_TgtIDField, this._SectionFilterComponent.TgtIDField);
                    this.SelectFieldInComboBox(this.cbo_ContentField, this._SectionFilterComponent.TgtContentField);
                    this.SelectFieldInComboBox(this.cbo_FlagField, this._SectionFilterComponent.FlagField);
                }
            }
        }
    }
}
